﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;

namespace RssApp.Parser
{
    /// <summary>
    /// 解析器
    /// </summary>
    public abstract class BlogParserBase
    {
        /// <summary>
        /// <![CDATA[获取RSS资源内容]]>
        /// </summary>
        /// <param name="rss">RSS</param>
        /// <returns></returns>
        protected virtual string TryGetRss(Rss.Domain.RssInfo rss)
        {
            string strResponseText = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rss.Url);
                request.Method = "GET";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
                request.Timeout = 30000;
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        strResponseText = streamReader.ReadToEnd();
                        strResponseText = strResponseText.Substring(strResponseText.IndexOf("<"));
                    }
                }
            }
            catch (System.Net.WebException)
            {
                return strResponseText;
            }
            return strResponseText;
        }

        /// <summary>
        /// 异步方法:获取Rssw
        /// </summary>
        /// <param name="rss"></param>
        /// <returns></returns>
        protected async Task<string> TryGetRssAsync(Rss.Domain.RssInfo rss)
        {
            string strResponseText = string.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rss.Url);
                request.Method = "GET";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
                request.Timeout = 30000;
                using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                {
                    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        strResponseText = streamReader.ReadToEnd();
                        strResponseText = strResponseText.Substring(strResponseText.IndexOf("<"));
                    }
                }
            }
            catch (System.Net.WebException)
            {
                return await Task.FromResult<string>(strResponseText);
            }
            return await Task.FromResult<string>(strResponseText);
        }
    }
}